package com.example.eventreg.sgin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eventreg.Classes.*;
import com.example.eventreg.Student.Student;
import com.example.eventreg.Student.StudentMapper;
import com.example.eventreg.action.Action;
import com.example.eventreg.action.ActionMapper;
import com.example.eventreg.security.JwtUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.jsonwebtoken.security.Password;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequestMapping("/sgin")
@CrossOrigin
@RestController // 用于定义该类为一个RESTful API控制器
public class SginController {
    @Autowired
    private SginMapper sginMapper;
    @Autowired
    private ActionMapper actionMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private JwtUtil jwtUtil;
    public SginController(){}

    @GetMapping("/get")
    public List<Sgin> getAllSgin() {
        return sginMapper.selectList(null); // 返回200状态码和用户列表
    }

    //从活动获取
    @GetMapping("/getA/{aid}")//组织者
    public List<Sgin> getSginAid(@RequestHeader("Authorization") String token,@PathVariable Long aid) {
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        Long oid = Long.valueOf(jwtUtil.getUserIdFromToken(token));
        if (actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",String.valueOf(aid))).getOid().equals(oid)) {
            return sginMapper.selectList(new QueryWrapper<Sgin>().eq("aid",String.valueOf(aid))); // 返回200状态码和用户列表
        };
        return null;
    }
    @GetMapping("/getAdmin/{aid}")//管理员
    public List<Sgin> getSginAidAdmin(@PathVariable Long aid) {
        return sginMapper.selectList(new QueryWrapper<Sgin>().eq("aid",String.valueOf(aid))); // 返回200状态码和用户列表

    }
    //从学生获取
    @GetMapping("/getS/{sid}")
    public List<Sgin> getSginSid(@PathVariable String sid) {
        return sginMapper.selectList(new QueryWrapper<Sgin>().eq("sid",sid)); // 返回200状态码和用户列表
    }
    //获取自己的
    @GetMapping("/getSelf")
    public List<SginStr> getSginSelf(@RequestHeader("Authorization") String token) {
        System.out.println("InGetSginSelf");
        if (token != null) {
            // 去除 "Bearer " 前缀，获取纯净的 token
            if (token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            String sid = jwtUtil.getUserIdFromToken(token);
            List<Sgin> sginList = sginMapper.selectList(new QueryWrapper<Sgin>().eq("sid", sid)); // 返回200状态码和用户列表
            List<SginStr> sginStrList = new ArrayList<>();
            for (Sgin sgin : sginList) {
                SginStr sginStr = new SginStr();
                sginStr.setSid(sgin.getSid());
                sginStr.setAid(sgin.getAid());
                sginStr.setEvaluate(String.valueOf(sgin.getEvaluate()));
                sginStr.setReg_data(sgin.getReg_data());
                if(Objects.equals(sgin.getSign_data(), LocalDateTime.of(1, 1, 1, 0, 0)))
                {
                    sginStr.setSign_data("未签到");
                }
                else
                {
                    sginStr.setSign_data(String.valueOf(sgin.getSign_data()));
                }
                sginStrList.add(sginStr);
            }
            return sginStrList;
        }
        return null;
    }

    @GetMapping("/getAS")
    public List<Sgin> getSgin(@RequestBody SidAid obj) {
        String sid = obj.getSid();
        Long aid = obj.getAid();
        return sginMapper.selectList(new QueryWrapper<Sgin>().eq("sid",sid).eq("aid",String.valueOf(aid)));
    }

    //获取该组织者的所有活动
    @GetMapping("/getOrg")//组织者
    public List<Sgin> getSginOrgAll(@RequestHeader("Authorization") String token)
    {
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        String oid = jwtUtil.getUserIdFromToken(token);
        List<Action> actions = actionMapper.selectList(new QueryWrapper<Action>().eq("oid",oid));
        List<Sgin> sginList = new ArrayList<>();
        for (Action action : actions) {
            List<Sgin> tmp = sginMapper.selectList(new QueryWrapper<Sgin>().eq("aid",String.valueOf(action.getAid())));
            sginList.addAll(tmp);
        }
        return sginList;
    }



    //一般报名****学生活动
    @PostMapping("/reg")
    public String regSgin(@RequestBody SidAid obj) {
        String sid = obj.getSid();
        Long aid = obj.getAid();
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid));
        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("sid",sid));
        if(action!=null&&student!=null) {
            Sgin sgin = new Sgin();
            sgin.setSid(sid);
            sgin.setAid(aid);
            sgin.setReg_data(LocalDateTime.now());
            sgin.setSign_data(LocalDateTime.of(1 ,1,1,0,0));
            sgin.setEvaluate("");
            sginMapper.insert(sgin);
            return "Success";
        }
        return "Action Invalied";
    }
    //一般报名****活动（token获取sid）
    @PostMapping("/regS")
    public String regSginS(@RequestHeader("Authorization") String token,@RequestBody Long aid) {
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid));
        if(action!=null)
        {
            System.out.println("token: "+token);
            if (token != null) {
                // 去除 "Bearer " 前缀，获取纯净的 token
                if(token.startsWith("Bearer "))
                {
                    token = token.substring(7);
                }
                if (jwtUtil.getTypeFromToken(token).equals("S"))
                {
                    String sid = jwtUtil.getUserIdFromToken(token);
                    System.out.println("sid: "+sid);
                    Sgin sgin = new Sgin();
                    sgin.setSid(sid);
                    sgin.setAid(aid);
                    sgin.setReg_data(LocalDateTime.now());
                    sgin.setSign_data(LocalDateTime.of(1,1,1,0,0,0));
                    sgin.setEvaluate("");
                    sginMapper.insert(sgin);
                    return "Success";
                }
            }
        }
        return "Action Invalied";
    }

    //无密码签到
    @PostMapping("/sgin")
    public String sginSgin(@RequestBody SidAid obj) {
        String sid = obj.getSid();
        Long aid = obj.getAid();
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid));
        if(action!=null) {
            UpdateWrapper<Sgin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("sid", sid).eq("aid", String.valueOf(aid));
            Sgin sgin = new Sgin();
            sgin.setSign_data(LocalDateTime.now());
            sginMapper.update(sgin, updateWrapper);
            return "Success";
        }
        return "Action Invalied";
    }
    @PostMapping("/sginS")
    public Sgin sginSginS(@RequestHeader("Authorization") String token, @RequestBody Long aid) {
        System.out.println("token: "+token);
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid));
        if (token != null && action!=null) {
            // 去除 "Bearer " 前缀，获取纯净的 token
            if (token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            String sid = jwtUtil.getUserIdFromToken(token);
            UpdateWrapper<Sgin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("sid",sid).eq("aid",String.valueOf(aid));
            Sgin sgin = new Sgin();
            sgin.setSign_data(LocalDateTime.now());
            sginMapper.update(sgin, updateWrapper);
            return sgin;
        }
        return null;
    }
    //密码签到
    @PostMapping("/sginPwd")
    public String sginSginPwd(@RequestBody SidAidPwd obj) {
        String sid = obj.getSid();
        Long aid = obj.getAid();
        String password = obj.getPassword();
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid));
        if(action!=null && action.getPassword().equals(password))
        {
            UpdateWrapper<Sgin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("sid",sid).eq("aid",String.valueOf(aid));
            Sgin sgin = new Sgin();
            sgin.setSign_data(LocalDateTime.now());
            sginMapper.update(sgin, updateWrapper);
            return "Success";
        }
        return "Password";
    }
    @PostMapping("/sginPwdS")
    public String sginSginPwdS(@RequestHeader("Authorization") String token, @RequestBody AidPwd obj) {
        Long aid = obj.getAid();
        String password = obj.getPassword();
        System.out.println("token: "+token);
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid));
        if (action!=null && action.getPassword().equals(password)&&token != null) {
            // 去除 "Bearer " 前缀，获取纯净的 token
            if (token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            String sid = jwtUtil.getUserIdFromToken(token);
            UpdateWrapper<Sgin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("sid",sid).eq("aid",String.valueOf(aid));
            Sgin sgin = new Sgin();
            sgin.setSign_data(LocalDateTime.now());
            sginMapper.update(sgin, updateWrapper);
            return "Success";
        }
        return null;
    }
    //签到
    @PostMapping("/verifySignCode")
    public SginResponse SginPwdCheck(@RequestBody AidPwd obj) {
        Long aid = obj.getAid();
        String password = obj.getPassword();
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid).eq("password",password));
        System.out.println("aid: "+aid);
        System.out.println("password: "+password);
        if (action!=null)
        {
            System.out.println("success");
            return new SginResponse("Success");
        }
        return null;
    }
    @PostMapping("/in")
    public String sginSginIn(@RequestHeader("Authorization") String token ,@RequestBody Aid obj) {
        Long aid = obj.getAid();
        // 去除 "Bearer " 前缀，获取纯净的 token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        String sid = jwtUtil.getUserIdFromToken(token);
        System.out.println("Aid: " + aid + "     Sid: " + sid);
        Action action = actionMapper.selectOne(new QueryWrapper<Action>().eq("aid",aid));
        if(action!=null) {
            UpdateWrapper<Sgin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("sid", sid).eq("aid", String.valueOf(aid));
            Sgin sgin = sginMapper.selectOne(updateWrapper);
            sgin.setSign_data(LocalDateTime.now());
            sginMapper.update(sgin, updateWrapper);
            return "Success";
        }
        return "Action Invalied";
    }
    //评价
    @PostMapping("/evaluate")
    public Sgin evaluateSgin(@RequestBody SidAidEval obj) {
        String sid = obj.getSid();
        Long aid = obj.getAid();
        String evaluate = obj.getEvaluate();
        UpdateWrapper<Sgin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("sid",sid).eq("aid",String.valueOf(aid));
        Sgin sgin = sginMapper.selectOne(updateWrapper);
        sgin.setEvaluate(evaluate);
        sginMapper.update(sgin, updateWrapper);
        return sgin;
    }
    //全自定参数报名
    @PostMapping("/regFull")
    public Sgin createSgin(@RequestBody Sgin sgin) {
        sginMapper.insert(sgin);
        return sgin;
    }
    //更新
    @PutMapping("/update")
    public int updateOrg(@RequestBody Sgin action) {
        return sginMapper.updateById(action);
    }


}
